//
//  YZOilLeftViewController.m
//  ZhuKuang
//
//  Created by jayce on 17/7/12.
//  Copyright © 2017年 jjkj. All rights reserved.
//

#import "YZOilLeftViewController.h"
#import "YZOilTankView.h"
#import "YZHomeInfoApi.h"
#import "YZHomeInfoModel.h"

@interface YZOilLeftViewController (){
    
    YZHomeInfoModel *yzHomeInfoModel;
}

- (IBAction)p_back:(id)sender;

@property (nonatomic, copy) NSString *startTime;
@property (nonatomic, copy) NSString *endTime;

@end

@implementation YZOilLeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    //获取油桶数据
    [self getYZHomeInfoWithStartTime:_startTime endTime:_endTime];
}

#pragma mark - 网络请求
- (void)getYZHomeInfoWithStartTime:(NSString *)startTime endTime:(NSString *)endTime{
    
    __weak typeof(self) weakself = self;
    NSDictionary *postDic = @{
                              @"startime":startTime,
                              @"endtime" :endTime,
                              @"iolId":kIolID
                              };
    [YZHomeInfoApi getYZHomeInfo:postDic success:^(NSDictionary *response) {
        yzHomeInfoModel = [YZHomeInfoModel mj_objectWithKeyValues:response];
        [weakself p_initTanksWith:yzHomeInfoModel.iolList];
    }];
    
}

#pragma mark - 重写时间选择方法
- (void)DateSelectedButtonClick:(UIButton *)sender{
    
    __weak typeof(self) weakself = self;
    
    if (!self.dateView) {
        
        self.dateView = [[ChooseDateView alloc]initWithFrame:self.view.bounds];
        self.dateView.backgroundColor = [UIColor whiteColor];
        [self.dateView setHidden:NO];
        
        self.dateView.selectDateCompletionHandle = ^(NSString *beginTime, NSString *endTime){
            
            NSLog(@"%@ - %@",beginTime, endTime);

            [weakself setRequestStartTime:beginTime endTime:endTime];
            [weakself getYZHomeInfoWithStartTime:weakself.startTime endTime:weakself.endTime];
        };
        
        [self.view addSubview:self.dateView];
    }
    else{
        
        [self.dateView setHidden:NO];
    }
}

-(void)p_initTanksWith:(NSArray *)tankArr {
    
    if (tankArr.count <=0) return;
    
    for (int i=0; i<tankArr.count; i++) {
        NSInteger gapX = 70;
        if (i%2 == 1) {
            gapX = kScreenWidth-155;
        }
        NSInteger gapY = 74+194*(i/2);
        YZOilTankView *tankView =  [YZOilTankView viewFromXib];;
        tankView.frame = CGRectMake(gapX, gapY, 85, 184);
        tankView.yzOilTankModel = [tankArr objectAtIndex:i];
        [self.view addSubview:tankView];
    }
}
- (IBAction)p_back:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)setRequestStartTime:(NSString *)start endTime:(NSString *)end{
    
    self.startTime = start;
    self.endTime = end;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
